﻿using Coldairarrow.Entity.Base_AutoReplys;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base_AutoReplys
{
    public class Base_AutoReplyDetailBusiness : BaseBusiness<Base_AutoReplyDetail>, IBase_AutoReplyDetailBusiness, ITransientDependency
    {
        public Base_AutoReplyDetailBusiness(IDbAccessor repository)
            : base(repository)
        {
        }

        #region 外部接口

        public async Task<PageResult<Base_AutoReplyDetail>> GetDataListAsync(PageInput<GetMMSearchDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<Base_AutoReplyDetail>();
            var search = input.Search;

            //筛选
            if (!search.Search.IsNullOrEmpty())
            {
                where = where.And(x => x.Contents.Contains(search.Search));
            }

            if (!search.MainId.IsNullOrEmpty())
            {
                where = where.And(x => x.ReplyId == search.MainId);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<Base_AutoReplyDetail> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(Base_AutoReplyDetail data)
        {
            var aCount = GetIQueryable().Where(x => x.KeyNum.Equals(data.KeyNum) && x.ReplyId.Equals(data.ReplyId)).Count();
            if (aCount > 0)
            {
                throw new BusException("该标题内容已存在，请修改后重试！");
            }

            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(Base_AutoReplyDetail data)
        {
            var aCount = GetIQueryable().Where(x => x.KeyNum.Equals(data.KeyNum) && x.ReplyId.Equals(data.ReplyId) && !x.Id.Equals(data.Id)).Count();
            if (aCount > 0)
            {
                throw new BusException("该标题内容与其他重复，请修改后重试！");
            }
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}